package com.zoffy.wmsbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zoffy.wmsbackend.common.BaseResponse;
import com.zoffy.wmsbackend.common.RequestParam;
import com.zoffy.wmsbackend.common.ResultUtils;
import com.zoffy.wmsbackend.model.dto.cost.CostDTO;
import com.zoffy.wmsbackend.model.entity.Cost;
import com.zoffy.wmsbackend.model.entity.Goods;
import com.zoffy.wmsbackend.model.entity.StockOutOrderDetail;
import com.zoffy.wmsbackend.model.entity.Warehouse;
import com.zoffy.wmsbackend.model.vo.CostVO;
import com.zoffy.wmsbackend.service.CostService;
import com.zoffy.wmsbackend.service.GoodsService;
import com.zoffy.wmsbackend.service.StockOutOrderDetailService;
import com.zoffy.wmsbackend.service.WarehouseService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@PreAuthorize("hasAuthority('admin')")
@RequestMapping("/cost")
public class CostController {
    @Resource
    private CostService costService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private StockOutOrderDetailService stockOutOrderDetailService;
    @Resource
    private GoodsService goodsService;

    @PostMapping("/list")
    public BaseResponse<IPage<CostVO>> list(@RequestBody RequestParam<CostDTO> requestParam) {
        IPage<CostVO> res = costService.listCostPage(requestParam);
        return ResultUtils.success(res);
    }
    @Scheduled(cron = "0 0 0 * * *")
    public void calculate() {
        List<Cost> res=new ArrayList<>();
        Map<Long,Integer> goodsMap = goodsService.list(new QueryWrapper<Goods>()).stream().collect(Collectors.toMap(Goods::getId,Goods::getAmountGd));
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String currentTime = sdf.format(now);
        List<Warehouse> warehouseList = warehouseService.list();
        for (Warehouse warehouse : warehouseList) {
            QueryWrapper<StockOutOrderDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("warehouse_id", warehouse.getId());
            queryWrapper.le("create_time", currentTime);
            queryWrapper.gt(warehouse.getLastSpendTime()!=null,"create_time", warehouse.getLastSpendTime());
            List<StockOutOrderDetail> stockOutOrderDetails = stockOutOrderDetailService.list(queryWrapper);
            BigDecimal money = BigDecimal.ZERO;
            for (StockOutOrderDetail stockOutOrderDetail : stockOutOrderDetails) {
                money=money.add(new BigDecimal(stockOutOrderDetail.getQuantity()*goodsMap.get(stockOutOrderDetail.getGoodsId())));
            }
            if (!money.equals(BigDecimal.ZERO)) {
                Cost cost=new Cost();
                cost.setWarehouseId(warehouse.getId());
                cost.setManufacturerId(warehouse.getManufacturerId());
                cost.setMoney(money.toString());
                res.add(cost);
            }
            warehouse.setLastSpendTime(currentTime);
            warehouseService.updateById(warehouse);
        }
        costService.saveBatch(res);
    }

}